package se.webbzon.oschi01.nbt;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import se.webbzon.oschi01.singleton.Singleton;

public class ByteArrayTagFactory extends AbstractTagFactory<byte[]> implements Singleton {

	private ByteArrayTagFactory() {
		super(Tags.TAG_BYTE_ARRAY, "TAG_Byte_Array");
	}

	@Override public ByteArrayTag readTag(String tagName, DataInputStream is) throws IOException {
		int size = is.readInt();
		byte[] data = new byte[size];
		is.readFully(data);
		return new ByteArrayTag(this, tagName, data);
	}

	@Override public void writeTag(Tag<byte[]> tag, DataOutputStream dos) throws IOException {
		byte[] data = tag.getValue();
		int size = data.length;
		dos.writeInt(size);
		dos.write(data);
	}

	@Override public Tag<byte[]> createTag(String tagName) {
		return new ByteArrayTag(this, tagName, new byte[0]);
	}

}
